// pages/my/my.js
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
import request from '../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:{
      avatarUrl:defaultAvatarUrl,
    },
    nickName:'',
    hasUserInfo:false
  },

  toMyInfo(){
    wx.navigateTo({
      url:'/pages/my-info/my-info?nickName='+this.data.nickName
    })
  },
  toVip(){
    wx.navigateTo({
      url: '/pages/member-info/member-info',
    })
  },
  toAbout(){
    wx.navigateTo({
      url: '/pages/about/about',
    })
  },
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail 
    this.setData({
     ['userInfo.avatarUrl']:avatarUrl
    })
  },
getUserProfile(e) {
  wx.getUserProfile({
    desc: '展示用户信息', 
    success: (res) => {
      this.setData({
        userInfo: res.userInfo,
        nickName:res.userInfo.nickName,
        hasUserInfo: true
      })
      wx.setStorageSync('name', res.userInfo.nickName)
      wx.login({
        success(res) {
          if (res.code) {
           request({
             url:'/auth/login',
             method:'POST',
             data:{
              code:res.code
             }
           }).then(res=>{
             if(res.code === 200){
              wx.setStorage({
                key:'token',
                data:res.data.token
              })
             }else{
               console.log(res.message);
             }
           })
          } else {
            console.log('登录失败！' + res.errMsg)
          }
        }
      })
    },
  })
},
setfieldValue(){
  let hasUserInfo = this.data.hasUserInfo
  if(!hasUserInfo){
    wx.removeStorageSync('sex')
    wx.removeStorageSync('name')
    wx.removeStorageSync('phone')
    // wx.removeStorageSync('token')
  }
},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let nickName = wx.getStorageSync('name')
    this.setData({
      nickName
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})